package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.TicketEntity;

/**
 * Created by qiuzhenhuan on 2019/7/3.
 */

public class ADA_CouponList extends CommonAdapter<TicketEntity> {

    private List<TicketEntity> mSelectedItemList;

    private boolean isDoubleCheck = true;

    public List<TicketEntity> getSelectedItemList() {
        return ListUtil.deepCopy(mSelectedItemList);
    }

    public ADA_CouponList(Context context) {
        super(context);
        mSelectedItemList = new ArrayList<>();
    }

    private boolean isEnableSelected;

    public void setEnableSelected(boolean enableSelected) {
        isEnableSelected = enableSelected;
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_coupon;
    }

    @Override
    protected void convert(ViewHolder holder, final TicketEntity item, final int position) {

        // 设置基本数据

        StringBuffer couponAvailableTime = new StringBuffer();
        couponAvailableTime
                .append(AppDateUtil.getTimeStamp(item.getStartDate(), AppDateUtil.YYYY_MM_DD2))
                .append(" - ")
                .append(AppDateUtil.getTimeStamp(item.getEndDate(), AppDateUtil.YYYY_MM_DD2));

        holder.setText(R.id.tv_ticket_last_time, String.format("有效期：%s", couponAvailableTime.toString()));
        holder.setText(R.id.tv_ticket_name, StringUtils.get(item.getTicketName()));

        RadioButton rbSelect = holder.getView(R.id.rb_coupon_select);
        rbSelect.setClickable(false);
        holder.setVisible(R.id.rb_coupon_select, isEnableSelected);
        holder.setChecked(R.id.rb_coupon_select, checkTicketIsSelected(item) != null);
        TextView tvUtility = holder.getView(R.id.tv_coupon_utility);

        // 根据优惠券类型设置条目样式展示
        switch (item.getTicketType()) {
            case TicketEntity.TICKET_TYPE_DISCOUNT:
                String condition = "[满" + BigDecimalUtil.getNumber(item.getLimitAmount()) + "元可用]";
                holder.setVisible(R.id.tv_coupon_condition, true);
                holder.setText(R.id.tv_coupon_condition, condition);
                holder.setText(R.id.tv_ticket_type, "满减券");
                tvUtility.setVisibility(View.INVISIBLE);
                holder.setText(R.id.tv_ticket_amount, String.format("￥%s", item.getTicketAmount()));

                break;
            case TicketEntity.TICKET_TYPE_EXPERIMENT:
                holder.setText(R.id.tv_ticket_type, "体验券");
                tvUtility.setText(item.getServiceName());
                holder.setVisible(R.id.tv_coupon_condition, false);
                tvUtility.setVisibility(View.VISIBLE);
                holder.setText(R.id.tv_ticket_amount, String.format("￥%s", item.getTicketAmount()));

                break;
            case TicketEntity.TICKET_TYPE_COUPON_SETTLE:
                holder.setText(R.id.tv_ticket_type, "折扣券");
                tvUtility.setText(StringUtils.get(item.getServiceName(), "折扣券"));
                holder.setVisible(R.id.tv_coupon_condition, false);
                tvUtility.setVisibility(View.INVISIBLE);
                holder.setText(R.id.tv_ticket_amount, String.format("%s折", item.getTicketDiscount()));
                break;
        }
        // 设置条目点击事件
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isEnableSelected)
                    return;

                TicketEntity listBean = checkTicketIsSelected(item);
                if (isDoubleCheck) {

                    if (listBean != null) {
                        mSelectedItemList.remove(listBean);
                    } else {
                        mSelectedItemList.add(item);
                    }
                    notifyDataSetChanged();
                } else {
                    if (listBean != null) {
                        mSelectedItemList.remove(listBean);
                    } else {
                        mSelectedItemList.clear();
                        mSelectedItemList.add(item);
                    }
                    notifyDataSetChanged();
                }

                if (mListener != null) {
                    mListener.onItemSelected(listBean);
                }
            }
        });

    }

    private String getCouponUtility(TicketEntity item) {
        StringBuilder buffer = new StringBuilder();

        List<TicketEntity.ServicesBean> serviceList = item.getServiceList();
        for (TicketEntity.ServicesBean servicesBean : serviceList) {
            buffer.append(servicesBean.getServiceName()).append(" ");
        }
        return buffer.toString().trim();
    }

    private TicketEntity checkTicketIsSelected(TicketEntity ticket) {
        for (TicketEntity bean : mSelectedItemList) {
            if (bean.getId().equals(ticket.getId())) {
                return bean;
            }
        }
        return null;
    }

    public void clearCheckStatus() {
        mSelectedItemList.clear();
    }

    public void setSelectedItemList(List<TicketEntity> mSelectedItemList) {
        this.mSelectedItemList = mSelectedItemList;
        notifyDataSetChanged();
    }


    public interface OnItemSelectedListener {
        void onItemSelected(TicketEntity item);
    }

    private ADA_CouponList.OnItemSelectedListener mListener;

    public void setOnItemSelectedListener(ADA_CouponList.OnItemSelectedListener mListener) {
        this.mListener = mListener;
    }
}
